//
//  WHDialog.m
//  WHPersonalData
//
//  Created by hyf on 2023/3/21.
//  Copyright © 2023 mac. All rights reserved.
//

#import "WHDialog.h"
#import "SVProgressHUD.h"
#import "WHWproxyKitUtility.h"

#define bgDialogColor RGBColor(61, 61, 61)
#define bgDialogButtonColor RGBColor(30, 144, 255)

@interface WHDialog()

@end

@implementation WHDialog

#pragma mark - 单例模式
+ (instancetype)sharedManager{
      static WHDialog *_instance;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
          _instance = [[WHDialog alloc] init];
    });
   return _instance;
}

#pragma mark 显示转圈的加载框
- (WMZDialog *)showHudWithText:(NSString*)text{
    [SVProgressHUD dismiss];
    if(self.mDialog){
        [self closeAllDialogWithDialog:self.mDialog];
    }
    self.mDialog = Dialog()
    .wTitleSet(text)
    .wTypeSet(DialogTypeLoading)
    //遮罩层是否显示
    .wShadowShowSet(NO)
    //展示右上角关闭按钮
    .wShowCloseSet(YES)
    //是否自动关闭
    .wAutoCloseSet(NO)
    //防止重复出现
    .wTagSet(1)
    //NO:可以交互 YES:不可交互
    .wUserInteractionEnabledSet(YES)
    //加载框type
    .wLoadingTypeSet(LoadingStyleSystem)
    //加载框图片大小
    .wLoadingSizeSet(CGSizeMake(60, 60))
    //背景颜色
    .wMainBackColorSet(bgDialogColor)
    ////弹框父试图frame
    .wDialogRectSet(CGRectMake(0,DialogNaviH,DialogScreenW,DialogScreenH-DialogNaviH))
    .wEventCancelFinishSet(^(id anyID, id otherData) {
        [self closeAllDialogWithDialog:self.mDialog];
        if(self.mDialog.mWproxyId.length>0){
            [WHWproxyKitUtility cancelRequestWithIdentifierWithID:self.mDialog.mWproxyId];
            self.mDialog.mWproxyId = @"";
        }
        if(self.mDialog.closeDialogBlock){
            self.mDialog.closeDialogBlock();
        }
    })
    .wStart();
    return self.mDialog;
}

#pragma mark 显示错误的加载框
- (WMZDialog *)showHudErrorWithText:(NSString*)text{
    [SVProgressHUD dismiss];
    if(self.mDialog){
        [self closeAllDialogWithDialog:self.mDialog];
    }
    self.mDialog = Dialog()
    .wTitleSet(text)
    .wTypeSet(DialogTypeLoading)
    //加载框颜色
    .wLoadingColorSet([UIColor whiteColor])
    //遮罩层是否显示
    .wShadowShowSet(NO)
    //展示右上角关闭按钮
    .wShowCloseSet(NO)
    //防止重复出现
    .wTagSet(2)
    //NO:可以交互 YES:不可交互
    .wUserInteractionEnabledSet(YES)
    //加载框type
    .wLoadingTypeSet(LoadingStyleError)
    //动画时间
    .wAnimationDurtionSet(0.5)
    //加载框图片大小
    .wLoadingSizeSet(CGSizeMake(40, 40))
    .wWidthSet(150)
    .wMainOffsetYSet(DialogRealW(40))
    //背景颜色
    .wMainBackColorSet(bgDialogColor)
    .wStart();
    return self.mDialog;
}

#pragma mark 显示成功的加载框
- (WMZDialog *)showHudSuccessWithText:(NSString*)text{
    [SVProgressHUD dismiss];
    if(self.mDialog){
        [self closeAllDialogWithDialog:self.mDialog];
    }
    self.mDialog = Dialog()
    .wTitleSet(text)
    .wTypeSet(DialogTypeLoading)
    //加载框颜色
    .wLoadingColorSet([UIColor whiteColor])
    //遮罩层是否显示
    .wShadowShowSet(NO)
    //展示右上角关闭按钮
    .wShowCloseSet(NO)
    //防止重复出现
    .wTagSet(3)
    //NO:可以交互 YES:不可交互
    .wUserInteractionEnabledSet(YES)
    //加载框type
    .wLoadingTypeSet(LoadingStyleRight)
    //动画时间
    .wAnimationDurtionSet(0.5)
    //加载框图片大小
    .wLoadingSizeSet(CGSizeMake(40, 40))
    .wWidthSet(150)
    .wMainOffsetYSet(DialogRealW(40))
    //背景颜色
    .wMainBackColorSet(bgDialogColor)
    .wStart();
    return self.mDialog;
}

#pragma mark 显示警告的加载框
- (WMZDialog *)showHudAlertWithText:(NSString*)text{
    [SVProgressHUD dismiss];
    if(self.mDialog){
        [self closeAllDialogWithDialog:self.mDialog];
    }
    self.mDialog = Dialog()
    .wTitleSet(text)
    .wTypeSet(DialogTypeLoading)
    //加载框颜色
    .wLoadingColorSet([UIColor whiteColor])
    //遮罩层是否显示
    .wShadowShowSet(NO)
    //展示右上角关闭按钮
    .wShowCloseSet(NO)
    //防止重复出现
    .wTagSet(4)
    //NO:可以交互 YES:不可交互
    .wUserInteractionEnabledSet(YES)
    //加载框type
    .wLoadingTypeSet(LoadingStyleInfo)
    //动画时间
    .wAnimationDurtionSet(0.5)
    //加载框图片大小
    .wLoadingSizeSet(CGSizeMake(40, 40))
    .wWidthSet(150)
    .wMainOffsetYSet(DialogRealW(40))
    //背景颜色
    .wMainBackColorSet(bgDialogColor)
    .wStart();
    return self.mDialog;
}

#pragma mark 显示进度条的加载框
- (WMZDialog *)showProgressHUDWithText:(NSString*)text{
    [SVProgressHUD dismiss];
    if(self.mDialog){
        [self closeAllDialogWithDialog:self.mDialog];
    }
    self.mDialog = Dialog()
    .wTitleSet(text)
    .wTypeSet(DialogTypeDown)
    .wImageNameSet(@"down_tyx")
    .wEventFinishSet(^(id anyID, NSIndexPath *path, DialogType type) {
        NSLog(@"%@",anyID);
    })
    .wDataSet(@(0.0))
    //遮罩层能否点击
    .wShadowCanTapSet(NO)
    //NO:可以交互 YES:不可交互 view?YES:NO
    .wUserInteractionEnabledSet(YES)
    //展示右上角关闭按钮
    .wShowCloseSet(YES)
    //遮罩层是否显示
    .wShadowShowSet(NO)
    //背景颜色
    .wMainBackColorSet(bgDialogColor)
    //标题颜色
    .wTitleColorSet([UIColor whiteColor])
    //内容颜色
    .wMessageColorSet([UIColor whiteColor])
    //弹框父试图frame
    .wDialogRectSet(CGRectMake(0,DialogNaviH,DialogScreenW,DialogScreenH-DialogNaviH))
    .wWidthSet(200)
    .wEventCancelFinishSet(^(id anyID, id otherData) {
        [self closeAllDialogWithDialog:self.mDialog];
        if(self.mDialog.mWproxyId.length>0){
            [WHWproxyKitUtility cancelRequestWithIdentifierWithID:self.mDialog.mWproxyId];
        }
        if(self.mDialog.closeDialogBlock){
            self.mDialog.closeDialogBlock();
        }
    })
    .wStart();
    return self.mDialog;
}

#pragma mark 显示选择弹框
- (void)showListSelectWithText:(NSString*)text data:(NSArray *)data OKFinishBlock:(void (^)(id anyID,id otherData)) oKFinishBlock wCustomCellBlock:(UITableViewCell* (^)(NSIndexPath* indexPath,UITableView* tableView,id model,BOOL isSelected)) wCustomCellBlock{
    Dialog()
        .wTypeSet(DialogTypeSelect)
        //取消按钮
        .wEventCancelFinishSet(^(id anyID, id otherData) {
            
        })
        //确定按钮
        .wEventOKFinishSet(oKFinishBlock)
        //自定义cell
        .wCustomCellSet(wCustomCellBlock)
        //显示动画
        .wShowAnimationSet(AninatonShowNone)
        //标题文字
        .wTitleSet(text)
        //文字大小
        .wTitleFontSet(16)
        //列表数据
        .wDataSet(data)
        //多选
        .wMultipleSelectionSet(YES)
        //确定按钮颜色
        .wOKColorSet(bgDialogButtonColor)
        //返回按钮颜色
        .wCancelColorSet(bgDialogButtonColor)
        //遮罩层能否点击
        .wShadowCanTapSet(NO)
        .wStart();
}

#pragma mark 显示单选选择弹框
- (void)showListSingleSelectWithText:(NSString*)text
                                data:(NSArray *)data
                       OKFinishBlock:(void (^)(id anyID,id otherData)) oKFinishBlock
                    wCustomCellBlock:(UITableViewCell* (^)(NSIndexPath* indexPath,UITableView* tableView,id model,BOOL isSelected)) wCustomCellBlock
                    wSelectCellBlock:(void (^)(NSIndexPath* indexPath,UITableView* tableView,id model))wSelectCellBlock{
    Dialog()
        .wTypeSet(DialogTypeSelect)
        //单选
        .wMultipleSelectionSet(YES)
        //取消按钮
        .wEventCancelFinishSet(^(id anyID, id otherData) {
            
        })
        //确定按钮
        .wEventOKFinishSet(oKFinishBlock)
        //自定义cell
        .wCustomCellSet(wCustomCellBlock)
        //自定义cell点击事件
        .wSelectCellSet(wSelectCellBlock)
        //显示动画
        .wShowAnimationSet(AninatonShowNone)
        //标题文字
        .wTitleSet(text)
        //文字大小
        .wTitleFontSet(16)
        //列表数据
        .wDataSet(data)
        //确定按钮颜色
        .wOKColorSet(bgDialogButtonColor)
        //返回按钮颜色
        .wCancelColorSet(bgDialogButtonColor)
        //遮罩层能否点击
        .wShadowCanTapSet(NO)
        .wStart();
}

#pragma mark 显示带自定义图片的弹框
- (void)showHUDImgWithText:(NSString*)text image:(UIImage*)image{
    [SVProgressHUD  showImage:image status:text];
}

#pragma mark 修改进度条
- (void)updateAlertTypeDownProgress:(CGFloat)value Dialog:(WMZDialog *) dialog{
    [dialog updateAlertTypeDownProgress:value];
}

#pragma mark 关闭进度弹框
- (void)dismissWithDialog:(WMZDialog *) dialog{
    [SVProgressHUD dismiss];
    [dialog closeView];
//    if(self.mDialog.mWproxyId.length>0){
//        [WHWproxyKitUtility cancelRequestWithIdentifierWithID:self.mDialog.mWproxyId];
//        self.mDialog.mWproxyId = @"";
//    }
}

#pragma mark 关闭弹框
//wAutoCloseSet(NO) 自动关闭设置为no后要调这个
- (void)dismissWithView{
    [SVProgressHUD dismiss];
    [WMZDialog closeWithshowView:nil tag:1 block:nil];
//    if(self.mDialog.mWproxyId.length>0){
//        [WHWproxyKitUtility cancelRequestWithIdentifierWithID:self.mDialog.mWproxyId];
//        self.mDialog.mWproxyId = @"";
//    }
}

#pragma mark 关闭所有弹框
- (void)closeAllDialogWithDialog:(WMZDialog *) dialog{
    if(dialog){
        [self dismissWithDialog:dialog];
    }else{
        [self dismissWithDialog:self.mDialog];
    }
    [self dismissWithView];
}

@end
